using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

/// <summary>
/// Summary description for News
/// </summary>
/// 

[ActiveRecord]
public class News : ActiveRecordBase<News>
{
    public static NHibernate.Expression.MatchMode Exact = NHibernate.Expression.MatchMode.Exact;
    public static NHibernate.Expression.MatchMode Anywhere = NHibernate.Expression.MatchMode.Anywhere;
    public static NHibernate.Expression.MatchMode End = NHibernate.Expression.MatchMode.End;
    public static NHibernate.Expression.MatchMode Start = NHibernate.Expression.MatchMode.Start;


    private int newsID;

    [PrimaryKey(PrimaryKeyType.Identity)]
    public int NewsID
    {
        get { return newsID; }
        set { newsID = value; }
    }

    private String newsTitle;

    [Property]
    public String NewsTitle
    {
        get { return newsTitle; }
        set { newsTitle = value; }
    }
    
    private String newsShort;

    [Property(ColumnType = "StringClob")]
    public String NewsShort
    {
        get { return newsShort; }
        set { newsShort = value; }
    }


    private String newsDetail;
    [Property(ColumnType = "StringClob")]


    public String NewsDetail
    {
        get { return newsDetail; }
        set { newsDetail = value; }
    }    

    private int typeID;

    [Property]
    public int TypeID
    {
        get { return typeID; }
        set { typeID = value; }
    }

    private String images;

    [Property]
    public String Images
    {
        get { return images; }
        set { images = value; }
    }

    private bool activeFlag;

    [Property]
    public bool ActiveFlag
    {
        get { return activeFlag; }
        set { activeFlag = value; }
    }


    private DateTime createdDate;

    [Property]
    public DateTime CreatedDate
    {
        get { return createdDate; }
        set { createdDate = value; }
    }
    private DateTime updatedDate;

    [Property]
    public DateTime UpdatedDate
    {
        get { return updatedDate; }
        set { updatedDate = value; }
    }
    private String createdBy;

    [Property]
    public String CreatedBy
    {
        get { return createdBy; }
        set { createdBy = value; }
    }
    private String updatedBy;

    [Property]
    public String UpdatedBy
    {
        get { return updatedBy; }
        set { updatedBy = value; }
    }

    public News(String newstitle, String newsshort, String newsdetail,int typeid,String images ,bool activeFlag, String createdBy)
	{
        this.newsTitle = newstitle;
        this.newsShort = newsshort;
        this.typeID = typeid;
        this.images = images;
        this.newsDetail = newsdetail;
        this.createdBy = createdBy;
        this.updatedBy = createdBy;
        this.createdDate = DateTime.Now;
        this.updatedDate = DateTime.Now;
        this.activeFlag = activeFlag;
	}

    public News()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static void delete_news(int newsid)
    {
        News news = get_news(newsid);
        news.Delete();       
    }


    public static void update_news(int newsID, String newstitle, String newsshort, String newsdetail, int typeid,String images, bool activeFlag, String updatedBy)
    {
        News news = new News();
        news = get_news(newsID);
        news.typeID = typeid;
        news.images = images;
        news.newsDetail = newsdetail;
        news.newsShort = newsshort;
        news.newsTitle = newstitle;
        news.activeFlag = activeFlag;
        news.UpdatedBy = updatedBy;
        news.updatedDate = DateTime.Now;
        news.Update();
    }

    public static void update_news(int newsID, bool activeFlag, String updatedBy)
    {
        News news = new News();
        news = get_news(newsID);
        news.activeFlag = activeFlag;
        news.UpdatedBy = updatedBy;
        news.updatedDate = DateTime.Now;
        news.Update();
    }

    public static void create_news(String newstitle, String newsshort, String newsdetail, int typeid,String images, bool activeFlag, String createdBy)
    {
        News news = new News(newstitle,newsshort,newsdetail,typeid,images, activeFlag, createdBy);
        news.Create();

    }

    public static News get_news(int newsid)
    {
        return FindByPrimaryKey(newsid);
    }

    public static News[] get_news_by_type(int typeid)
    {
        return FindAllByProperty("TypeID", typeid);
    }

    public static News[] get_all_news()
    {
        return FindAll();
    }

    public static int count_all_news()
    {
        return Count();
    }

    public static News[] search_news(String propertyName, String textSearch, NHibernate.Expression.MatchMode mode)
    {
        NHibernate.Expression.DetachedCriteria criteria = NHibernate.Expression.DetachedCriteria.For<News>();
        criteria.Add(NHibernate.Expression.Expression.InsensitiveLike(propertyName, textSearch, mode));
        return FindAll(criteria, News.Asc("NewsID"));
    }

    public static News[] get_news_and_sort(String propertyName,String order)
    {
        NHibernate.Expression.DetachedCriteria avgWeight = NHibernate.Expression.DetachedCriteria.For<News>();
        if (order=="ASC")
            return FindAll(avgWeight,Asc(propertyName));
        return FindAll(avgWeight,Desc(propertyName));
    }
}
